package com.fy.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 获取IP和mac地址
 **/
public class NetWorkUtils {

    /***
     * 获取机器的mac地址
     * @return
     */
    public static String getNetWorkInfo() {
        StringBuilder sb = new StringBuilder();
        try {
            InetAddress ia = InetAddress.getLocalHost();
            //获取网卡，获取地址
            byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
            for (int i = 0; i < mac.length; i++) {
                if (i != 0) {
                    sb.append("-");
                }
                //字节转换为整数
                int temp = mac[i] & 0xff;
                String str = Integer.toHexString(temp);

                if (str.length() == 1) {
                    sb.append("0").append(str);

                } else {
                    sb.append(str);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(getNetWorkInfo());
    }
}